#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>


//编译时 要是用 -pthread 选项
//gcc 10-pthread.c -o 10-pthread -pthread

void * pthread_handler1(void *arg)
{
    printf("arg1=%d\n",*(int *)arg);
    while(1)
    {
        printf("1: pthread_handler is running\n");
        sleep(1);
    }
}

void * pthread_handler2(void *arg)
{
    printf("arg2=%d\n",*(int *)arg);
    while(1)
    {
        printf("2: pthread_handler is running\n");
        sleep(1);
    }
}

void * pthread_handler3(void *arg)
{
    printf("arg3=%d\n",*(int *)arg);
    while(1)
    {
        printf("3: pthread_handler is running\n");
        sleep(1);
    }
}

int main(int argc, char const *argv[])
{
    //创建子线程1
    pthread_t tid1,tid2,tid3;//pthread id 线程 id
    int data1=100,data2=200,data3=300;
    int ret = pthread_create(&tid1,NULL,pthread_handler1,(void *)&data1);
    if (ret !=0)
    {
        printf("1: pthread_create eror\n");
    }

    //创建子线程2
    ret = pthread_create(&tid2,NULL,pthread_handler2,(void *)&data2);
    if (ret !=0)
    {
        printf("2: pthread_create eror\n");
    }

    //创建子线程3
    ret = pthread_create(&tid3,NULL,pthread_handler3,(void *)&data3);
    if (ret !=0)
    {
        printf("3: pthread_create eror\n");
    }

    printf("tid1=%ld\n",tid1);
    printf("tid2=%ld\n",tid2);
    printf("tid3=%ld\n",tid3);

    while (1)
    {
        printf("main pthread is running\n");
        sleep(1);
    }
    
    return 0;
}
